package com.jyb.model;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.IAtom;
import com.jfinal.plugin.activerecord.Model;
import com.jyb.enumeration.ShanghaiDistrict;
import com.jyb.util.StringUtils;
import com.jyb.util.UUIDUtils;
import com.jyb.util.capture.JxjhUtil;
import com.jyb.vo.JxjhVo;

public class Jxjh  extends Model<Jxjh>{

	
	public class IAtomJxjh implements IAtom{
		
		private ShanghaiDistrict district;
		
		public IAtomJxjh(ShanghaiDistrict district){
			this.district=district;
		}
		
		public boolean run() throws SQLException {
			
			List<JxjhVo> jxjhVos=JxjhUtil.capture2(district);
			if(jxjhVos.size()>0){
				String sql="delete from t_jxjh where gq=?";
				Db.update(sql,district.name());
				for (JxjhVo jxjhVo : jxjhVos) {
					Jxjh jxjh=new Jxjh();
					jxjh.set("id", UUIDUtils.getUUID());
					jxjh.set("createTime", new Date());
					jxjh.set("cm", jxjhVo.getCm());
					jxjh.set("ywcm", jxjhVo.getYwcm());
					jxjh.set("csi", jxjhVo.getCsi());
					jxjh.set("hc", jxjhVo.getHc());
					jxjh.set("jck", jxjhVo.getJck());
					jxjh.set("kg", jxjhVo.getKg());
					jxjh.set("jg", jxjhVo.getJg());
					jxjh.set("dl", jxjhVo.getDl());
					jxjh.set("gq", jxjhVo.getGq());
					jxjh.set("gqmc", jxjhVo.getGqmc());
					jxjh.save();
				}
			}
			
			return true;
		}  
	   
	}  
	
	private static final long serialVersionUID = 8477682563495887151L;
	public static final Jxjh dao = new Jxjh();
	
	public void updateJxjh(ShanghaiDistrict district){
		Db.tx(new IAtomJxjh(district));
	}
	
	public List<Jxjh> findListByCm(String cm,ShanghaiDistrict district){
		StringBuffer sql=new StringBuffer("select * from t_jxjh t where 1=1");
		List<Object> list=new ArrayList<Object>();
		if(!StringUtils.isEmpty(cm)){
			sql.append(" and (t.cm like ?  or t.ywcm like ? )");
			list.add("%"+cm+"%");
			list.add("%"+cm+"%");
		}
		if(district!=null){
			sql.append(" and t.gq = ? ");
			list.add(district.name());
		}
		return this.find(sql.toString(),list.toArray());
	}
}
